<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class UserModel extends JLModelBase {

    /**
     * Funcion que loguea al usuario
     * @Retorna array
     */
    public function Login($user, $pwd) {
        $_true = null;

        $sql = "SELECT 
			  tbl_users.int_id_user,
			  tbl_users.var_fullname,  
	  		  tbl_users.var_email,
			  tbl_users.var_username,
			  tbl_users.dat_creation,
			  tbl_users.int_id_section,
			  tbl_users.int_id_typeuser,
			  tbl_users.int_id_state,
			  tbl_sections.var_desc_section,
			  tbl_typeusers.var_desc_typeuser,
			  tbl_states.var_htmldesc
			FROM
			  tbl_sections
			  INNER JOIN tbl_users ON (tbl_sections.int_id_section = tbl_users.int_id_section)
			  INNER JOIN tbl_typeusers ON (tbl_users.int_id_typeuser = tbl_typeusers.int_id_typeuser)
			  INNER JOIN tbl_states ON (tbl_users.int_id_state = tbl_states.int_id_state)
			WHERE tbl_users.var_username = " . $this->DB->Quote($user) . " AND tbl_users.var_pwd = " . $this->DB->Quote($pwd) . " AND tbl_users.int_id_state <> 3";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
            $_true = $_true[0];
        }

        return $_true;
    }

    /**
     * Obtine usuarios usuario
     * @Retorna array
     */
    public function getUserById($id) {
        $_true = null;

        $sql = "SELECT 
            int_id_user,
            var_fullname,  
            var_email,
            var_pwd,
            var_username,
            dat_creation,
            int_id_section,
            int_id_typeuser,
            int_id_state
          FROM
            tbl_users
          WHERE int_id_user = " . $this->DB->Quote($id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
            $_true = $_true[0];
        }

        return $_true;
    }

    /**
     * Modifica usuario
     * @Retorna string
     */
    public function SaveUserUpdate($_id, $var_fullname, $var_email, $var_username, $var_pwd, $dat_modification, $int_id_section, $int_id_typeuser, $int_id_state) {
        $sql = "UPDATE 
			  `tbl_users`  
			SET 
			  `var_username` = " . $this->DB->Quote($var_username) . ",
			  `var_fullname` = " . $this->DB->Quote($var_fullname) . ",
	  		  `var_email` = " . $this->DB->Quote($var_email) . ",
			  `var_pwd` = " . $this->DB->Quote($var_pwd) . ",
			  `dat_modification` = " . $this->DB->Quote($dat_modification) . ",
			  `int_id_section` = " . $this->DB->Quote($int_id_section) . ",
			  `int_id_typeuser` = " . $this->DB->Quote($int_id_typeuser) . ",
			  `int_id_state` = " . $this->DB->Quote($int_id_state) . "
			WHERE 
			  `int_id_user` = " . $this->DB->Quote($_id) . " ";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = "Données sauvegarder avec succès.";
        } else {
            $_true = "Ne peut enregistrer vos données.";
        }

        return $_true;
    }

    /**
     * Crea un nuevo usuario
     * @Retorna string
     */
    public function SaveUser($var_fullname, $var_email, $var_username, $var_pwd, $dat_creation, $dat_modification, $int_id_section, $int_id_typeuser, $int_id_state) {
        $sql = "INSERT INTO 
			  `tbl_users`
			(
			  `var_username`,
			  `var_fullname`,  
			   `var_email`,
			  `var_pwd`,
			  `dat_creation`,
			  `dat_modification`,
			  `int_id_section`,
			  `int_id_typeuser`,
			  `int_id_state`
			) 
			VALUE (
			  " . $this->DB->Quote($var_username) . ",
			  " . $this->DB->Quote($var_fullname) . ",
			  " . $this->DB->Quote($var_email) . ",
			  " . $this->DB->Quote($var_pwd) . ",
			  " . $this->DB->Quote($dat_creation) . ",
			  " . $this->DB->Quote($dat_modification) . ",
			  " . $this->DB->Quote($int_id_section) . ",
			  " . $this->DB->Quote($int_id_typeuser) . ",
			  " . $this->DB->Quote($int_id_state) . ")";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = "Données sauvegarder avec succès.";
        } else {
            $_true = "Ne peut enregistrer vos données.";
        }

        return $_true;
    }

    /**
     * Elimina usuario
     * @Retorna string
     */
    public function DeleteUser($_id) {
        $sql = "DELETE FROM 
			  `tbl_users` 
			WHERE 
			  `int_id_user` = " . $this->DB->Quote($_id) . " AND `int_id_state` <> 1";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        } else {
            $_true = false;
        }

        return $_true;
    }

}